package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwbyT;
import cn.e4j.modular.crm.service.GwbyTService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/crm/gwai305")
public class Gwai305Controller extends BaseController {

    @Autowired
    GwbyTService gwbyTService;

    /**
     * 跳转至客户价值评估维护主界面
     *
     * @return
     */
    @RequiresPermissions("crm:gwai305:init")
    @RequestMapping(value = {"", "/init"})
    public String index() {
        return "/pc/crm/gwai305.html";
    }

    @RequiresPermissions("crm:gwai305:init")
    @RequestMapping("/getHeadList")
    @ResponseBody
    public ResponseData getHeadList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        List<Map<String, Object>> list = gwbyTService.getGwbylist(page, inDto);
        return new PageReData(page, list);
    }

    @RequiresPermissions("crm:gwai305:init")
    @RequestMapping("/getDetailList")
    @ResponseBody
    public ResponseData getDetailList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbyT> list = gwbyTService.getGwbyDetailList(inDto.getString("GWBY001"));
        list.stream().sorted(Comparator.comparing(GwbyT::getGWBY006)).collect(Collectors.toList());
        return new SuccessResponseData(list);
    }

    @RequiresPermissions("crm:gwai305:add")
    @RequestMapping("/add")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbyT> gwbyTList = inDto.getReqList(GwbyT.class);

        if (gwbyTService.existedTheParams(gwbyTList.get(0))) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        } else {
            gwbyTService.add(gwbyTList);
            return SUCCESS_TIP;
        }
    }

    @RequiresPermissions("crm:gwai305:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbyT> gwbyTList = inDto.getReqList(GwbyT.class);
        gwbyTService.edit(gwbyTList);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwai305:del")
    @RequestMapping("/del")
    @ResponseBody
    public ResponseData delRow(GwbyT gwbyT) {
        gwbyTService.delete(gwbyT);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwai305:del")
    @RequestMapping("/delBatch")
    @ResponseBody
    public ResponseData delRowsBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbyT> gwbyTList = inDto.getReqList(GwbyT.class);
        gwbyTService.deleteBatch(gwbyTList);
        return SUCCESS_TIP;
    }

    @RequiresPermissions("crm:gwai305:del")
    @RequestMapping("/getAddData")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        outDto.put("ID", DateUtils.formatDate(new Date(), "yyMMddSSSS"));
        outDto.put("CURRENT_USER_ID",shiroUser.getID());
        outDto.put("CURRENT_USER_NAME",shiroUser.getNAME());
        outDto.put("CURRENT_ORG",shiroUser.getORG_ID());
        outDto.put("CURRENT_ORG_NAME",shiroUser.getSysOrgT().getNAME());
        outDto.put("CURRENT_DATE", LocalDate.now());
        return new SuccessResponseData(outDto);
    }


    @RequiresPermissions("crm:gwai305:edit")
    @RequestMapping("/changeStatus")
    @ResponseBody
    public ResponseData changeStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbyT> gwbyTList = inDto.getReqList(GwbyT.class);
        gwbyTService.changeStatusBatch(gwbyTList);
        String state = gwbyTList.get(0).getGWBYACTI();
        // 更新客户信用等级
        if(state.equals("Y")){
            gwbyTService.updateGwak001(gwbyTList);
        }
        return SUCCESS_TIP;
    }

    @RequestMapping("/getGwbyData")
    @ResponseBody
    public ResponseData getGwbyData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        List<Map<String, Object>> list = gwbyTService.getGwbyData(page, inDto);
        return new PageReData(page, list);
    }
}
